package kr.co.sdsway2.web.client;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import kr.co.sdsway2.comm.SdsWay2Util;
import kr.co.sdsway2.common.AbstractContraller;
import kr.co.sdsway2.service.AvatarService;
import kr.co.sdsway2.service.PollAswInfBeanService;
import kr.co.sdsway2.service.PollAswService;
import kr.co.sdsway2.service.PollInfBeanService;
import kr.co.sdsway2.service.PtInfBeanService;
import kr.co.sdsway2.vo.AvatarVO;
import kr.co.sdsway2.vo.PollInfVO;
import kr.co.sdsway2.vo.PracticeDataVO;
import kr.co.sdsway2.vo.PtInfVO;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class RemoconController extends AbstractContraller {
	
	@Resource(name="avatarService")
	AvatarService avatarService;

	@Resource(name="ptInfBeanService")
	PtInfBeanService ptInfBeanService;
	
	@Resource(name="pollInfBeanService")
	PollInfBeanService pollInfBeanService;

	@Resource(name="pollAswInfBeanService")
	PollAswInfBeanService pollAswInfBeanService;

	@Resource(name="pollAswService")
	PollAswService pollAswService;
	
	@Resource(name="practiceInfBeanList")
	List<PracticeDataVO> practiceDataList;

	@RequestMapping("/remoconAjax")
	public void remoconAjax(Model model, HttpServletRequest req){
		
		String avatarId 	= 	req.getParameter("avatarId");
		String avatarType 	= 	req.getParameter("avatarType");
		int xDist 		=	Integer.parseInt(req.getParameter("xDist"))/10;	//	to reduce speed
		int yDist 		=	Integer.parseInt(req.getParameter("yDist"))/10; //	to reduce speed
		AvatarVO avatarVO = null;
		if(!SdsWay2Util.isNull(avatarId)){
			avatarVO = avatarService.getAvatar(avatarId);
			avatarService.move(avatarId, xDist, yDist,avatarType);
			avatarVO = avatarService.getAvatar(avatarId);
		}
		if(avatarVO==null) avatarVO = new AvatarVO();
		model.addAttribute("avatarVO", avatarVO);

		//"점수 (순위/총원)" 출력에 필요한 데이터 수집
		List<AvatarVO> tempAvatarVOList = avatarService.getList();
		AvatarVO tempAvatarVO = null;
		int rank=-1, total=0;
		if( tempAvatarVOList != null ) total = tempAvatarVOList.size();
		for(int i=0; i<total; i++) {
			tempAvatarVO = tempAvatarVOList.get(i);
			if( tempAvatarVO!=null && avatarId.equals(tempAvatarVO.getAvatarId())) {
				rank = i+1;
				break;
			}
		}
		model.addAttribute("rank", rank);
		model.addAttribute("total", total);

		//PT 상태 return
		PtInfVO ptInfVO = ptInfBeanService.getPtInfBean(1);
		if(ptInfVO!=null){
			Integer pstPollId = ptInfVO.getPstPollId();
			if(pstPollId!=null && pstPollId>0){
				PollInfVO pollInfVO = pollInfBeanService.getPollInfBean(1,pstPollId);
				if(pollInfVO!=null){
					model.addAttribute("pollInfVO", pollInfVO);
//					debug(">>>>> pollInfVO: "+pollInfVO);//[DELETED]
				}
			}
			
			if ("50".equals(ptInfVO.getPtStatCd())) {
				model.addAttribute("practiceDataList", practiceDataList);
			}
		}
		model.addAttribute("ptInfVO", ptInfVO);
	}
	
	@RequestMapping("/submitAsw")
	public void submitAsw(Model model, HttpServletRequest req){
		String ptId = req.getParameter("ptId");
		String pollId = req.getParameter("pollId");
		String avatarId = req.getParameter("avatarId");
		String aswVal = req.getParameter("aswVal");
		
//		debug(">>>>> ptId="+ptId+",pollId:"+pollId+",avatarId="+avatarId+",aswVal="+aswVal);//[DELETED]
		
		Integer intVal = pollAswService.updatePollAsw(Integer.parseInt(ptId), Integer.parseInt(pollId), avatarId, aswVal);
		if(intVal==1) model.addAttribute("successYn", "Y");
		else model.addAttribute("successYn", "N");
		
//		debug(">>>>> pollAswInfBeanList : "+pollAswInfBeanService.getList());//[DELETED]
	}
}
